package com.web.kftsdk.utils;

import com.web.kftsdk.bean.KftPayError;
import com.web.kftsdk.bean.MerchantTypeEnum;
import com.web.kftsdk.common.exception.PayErrorException;
import com.web.kftsdk.common.http.HttpRequestTemplate;
import com.web.kftsdk.common.util.str.StringUtils;

import com.web.kftsdk.entity.CertInfoEntity;
import com.web.kftsdk.entity.IdentificationReq;
import org.apache.commons.io.IOUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;

import java.io.*;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>Title: KftUtil</p>
 * <p>Description: TODO</p>
 *
 * @author xukk
 * @version 1.0
 * @date 2018/6/13
 */
public class KftUtil {
    public static List getCerts(HttpRequestTemplate requestTemplate, String filePath, MerchantTypeEnum merchantTypeEnum, IdentificationReq identificationReq){
        List<CertInfoEntity.Cert> certList=new ArrayList<>();
        int num=1;
        switch (merchantTypeEnum){
            case PERSONAL:
                if(StringUtils.isBlank(identificationReq.getP10())){
                    throw new PayErrorException(new KftPayError("uploadError","身份证正面照必填"));
                }else {
                    certList.add(CertInfoEntity.Cert.builder().no(""+num++).merchantType("1").certNo("10").
                            filePath(down2Local(requestTemplate,identificationReq.getP10(),filePath,"1")).build());
                }
                if(StringUtils.isBlank(identificationReq.getP11())){
                    throw new PayErrorException(new KftPayError("uploadError","身份证背面照必填"));
                }else {
                    certList.add(CertInfoEntity.Cert.builder().no(""+num++).merchantType("1").certNo("11")
                            .filePath(down2Local(requestTemplate,identificationReq.getP11(),filePath,"1")).build());
                }
                if(StringUtils.isBlank(identificationReq.getP12())){
                    throw new PayErrorException(new KftPayError("uploadError","手持身份证正面照必填"));
                }else {
                    certList.add(CertInfoEntity.Cert.builder().no(""+num++).merchantType("1").certNo("12")
                            .filePath(down2Local(requestTemplate,identificationReq.getP12(),filePath,"1")).build());
                }
                if(StringUtils.isBlank(identificationReq.getP13())){
                    throw new PayErrorException(new KftPayError("uploadError","银行卡正面照必填"));
                }else{
                    certList.add(CertInfoEntity.Cert.builder().no(""+num++).merchantType("1").certNo("13")
                            .filePath(down2Local(requestTemplate,identificationReq.getP13(),filePath,"1")).build());
                }
                if(StringUtils.isBlank(identificationReq.getP14())){
                    throw new PayErrorException(new KftPayError("uploadError","银行卡反面照必填"));
                }else{
                    certList.add(CertInfoEntity.Cert.builder().no(""+num++).merchantType("1").certNo("14")
                            .filePath(down2Local(requestTemplate,identificationReq.getP14(),filePath,"1")).build());
                }
                if(!StringUtils.isBlank(identificationReq.getP15())){
                    certList.add(CertInfoEntity.Cert.builder().no(""+num++).merchantType("1").certNo("15")
                            .filePath(down2Local(requestTemplate,identificationReq.getP15(),filePath,"1")).build());
                }
                if(StringUtils.isBlank(identificationReq.getP16())){
                    throw new PayErrorException(new KftPayError("uploadError","店面门头照必填"));
                }else{
                    certList.add(CertInfoEntity.Cert.builder().no(""+num++).merchantType("1").certNo("16")
                            .filePath(down2Local(requestTemplate,identificationReq.getP16(),filePath,"1")).build());
                }
                break;

            case ENTERPRISE:
                if(!StringUtils.isAnyNotBlank(identificationReq.getE21(),identificationReq.getE22())){
                    throw new PayErrorException(new KftPayError("uploadError","营业执照（三证合一）和营业执照二选一"));
                }
                if(!StringUtils.isBlank(identificationReq.getE21())){
                    certList.add(CertInfoEntity.Cert.builder().no(""+num++).merchantType("3").certNo("21")
                            .filePath(down2Local(requestTemplate,identificationReq.getE21(),filePath,"2")).build());
                }
                if(!StringUtils.isBlank(identificationReq.getE22())){
                    certList.add(CertInfoEntity.Cert.builder().no(""+num++).merchantType("3").certNo("22")
                            .filePath(down2Local(requestTemplate,identificationReq.getE22(),filePath,"2")).build());
                }
                if(!StringUtils.isAnyNotBlank(identificationReq.getE21(),identificationReq.getE23(),identificationReq.getE24())){
                    throw new PayErrorException(new KftPayError("uploadError","税务登记证、组织机构代码证二选一"));
                }
                if(!StringUtils.isBlank(identificationReq.getE23())){
                    certList.add(CertInfoEntity.Cert.builder().no(""+num++).merchantType("3").certNo("23")
                            .filePath(down2Local(requestTemplate,identificationReq.getE23(),filePath,"2")).build());
                }
                if(!StringUtils.isBlank(identificationReq.getE24())){
                    certList.add(CertInfoEntity.Cert.builder().no(""+num++).merchantType("3").certNo("24")
                            .filePath(down2Local(requestTemplate,identificationReq.getE21(),filePath,"2")).build());
                }
                if(StringUtils.isBlank(identificationReq.getE25())){
                    throw new PayErrorException(new KftPayError("uploadError","法人身份证正面照必填"));
                }else{
                    certList.add(CertInfoEntity.Cert.builder().no(""+num++).merchantType("3").certNo("25")
                            .filePath(down2Local(requestTemplate,identificationReq.getE25(),filePath,"2")).build());
                }
                if(StringUtils.isBlank(identificationReq.getE26())){
                    throw new PayErrorException(new KftPayError("uploadError","法人身份证反面照必填"));
                }else{
                    certList.add(CertInfoEntity.Cert.builder().no(""+num++).merchantType("3").certNo("26")
                            .filePath(down2Local(requestTemplate,identificationReq.getE26(),filePath,"2")).build());
                }
                if(StringUtils.isBlank(identificationReq.getE27())){
                    throw new PayErrorException(new KftPayError("uploadError","开户许可证或银行开户证明二选一"));
                }else{
                    certList.add(CertInfoEntity.Cert.builder().no(""+num++).merchantType("3").certNo("27")
                            .filePath(down2Local(requestTemplate,identificationReq.getE27(),filePath,"2")).build());
                }
                if(!StringUtils.isBlank(identificationReq.getE28())){
                    certList.add(CertInfoEntity.Cert.builder().no(""+num++).merchantType("3").certNo("28")
                            .filePath(down2Local(requestTemplate,identificationReq.getE28(),filePath,"2")).build());
                }
                if(StringUtils.isBlank(identificationReq.getE29())){
                    throw new PayErrorException(new KftPayError("uploadError","店面门头照必填"));
                }else{
                    certList.add(CertInfoEntity.Cert.builder().no(""+num++).merchantType("3").certNo("29")
                            .filePath(down2Local(requestTemplate,identificationReq.getE29(),filePath,"2")).build());
                }
                break;

            case INDIVIDUAL_BUSINESS:
                if(!StringUtils.isAnyNotBlank(identificationReq.getI30(),identificationReq.getI31())){
                    throw new PayErrorException(new KftPayError("uploadError","营业执照（三证合一）和营业执照二选一"));
                }
                if(!StringUtils.isBlank(identificationReq.getI30())){
                    certList.add(CertInfoEntity.Cert.builder().no(""+num++).merchantType("2").certNo("30")
                            .filePath(down2Local(requestTemplate,identificationReq.getI30(),filePath,"3")).build());
                }
                if(!StringUtils.isBlank(identificationReq.getI31())){
                    certList.add(CertInfoEntity.Cert.builder().no(""+num++).merchantType("2").certNo("31")
                            .filePath(down2Local(requestTemplate,identificationReq.getI31(),filePath,"3")).build());
                }
                if(StringUtils.isBlank(identificationReq.getI32())){
                    throw new PayErrorException(new KftPayError("uploadError","法人身份证正面照必填"));
                }else{
                    certList.add(CertInfoEntity.Cert.builder().no(""+num++).merchantType("2").certNo("32")
                            .filePath(down2Local(requestTemplate,identificationReq.getI32(),filePath,"3")).build());
                }
                if(!StringUtils.isBlank(identificationReq.getI33())){
                    certList.add(CertInfoEntity.Cert.builder().no(""+num++).merchantType("2").certNo("33")
                            .filePath(down2Local(requestTemplate,identificationReq.getI33(),filePath,"3")).build());
                }
                if(StringUtils.isBlank(identificationReq.getI34())){
                    throw new PayErrorException(new KftPayError("uploadError","银行卡正面照必填"));
                }else{
                    certList.add(CertInfoEntity.Cert.builder().no(""+num++).merchantType("2").certNo("34")
                            .filePath(down2Local(requestTemplate,identificationReq.getI34(),filePath,"3")).build());
                }
                if(StringUtils.isBlank(identificationReq.getI35())){
                    throw new PayErrorException(new KftPayError("uploadError","银行卡反面照必填"));
                }else{
                    certList.add(CertInfoEntity.Cert.builder().no(""+num++).merchantType("2").certNo("35")
                            .filePath(down2Local(requestTemplate,identificationReq.getI35(),filePath,"3")).build());
                }
                if(StringUtils.isBlank(identificationReq.getI36())){
                    throw new PayErrorException(new KftPayError("uploadError","手持身份证正面照必填"));
                }else{
                    certList.add(CertInfoEntity.Cert.builder().no(""+num++).merchantType("2").certNo("36")
                            .filePath(down2Local(requestTemplate,identificationReq.getI36(),filePath,"3")).build());
                }
                if(StringUtils.isBlank(identificationReq.getI37())){
                    throw new PayErrorException(new KftPayError("uploadError","店面门头照必填"));
                }else{
                    certList.add(CertInfoEntity.Cert.builder().no(""+num++).merchantType("2").certNo("37")
                            .filePath(down2Local(requestTemplate,identificationReq.getI37(),filePath,"3")).build());
                }
                if(!StringUtils.isBlank(identificationReq.getI38())){
                    certList.add(CertInfoEntity.Cert.builder().no(""+num++).merchantType("2").certNo("38")
                            .filePath(down2Local(requestTemplate,identificationReq.getI38(),filePath,"3")).build());
                }
                break;
            default:
        }
        return certList;
    }

    public static String down2Local(HttpRequestTemplate requestTemplate,String url,String filePath,String merchantType){
        try {
            HttpGet httpGet=new HttpGet(url);
            HttpResponse httpResponse=requestTemplate.getHttpClient().execute(httpGet);
            HttpEntity httpEntity=httpResponse.getEntity();
            InputStream in=httpEntity.getContent();
            String path="/cert/"+merchantType+"/"+System.currentTimeMillis()+".jpg";
            File file=new File(filePath+path);
            System.out.println(file.getAbsolutePath());
            if(!file.exists()){
               FileUtil.makeDirectory(file);
            }
            OutputStream outputStream=new FileOutputStream(file);
            byte[] buffer=new byte[1024];
            while((in.read(buffer))>0){
                outputStream.write(buffer);
            }
            outputStream.flush();
            in.close();
            outputStream.close();
            return path;
        } catch (IOException e) {
            throw new PayErrorException(new KftPayError("uploadError","文件下载转换时错误:"+e.getMessage()));
        }
    }

    public static void generateInfo(String url,CertInfoEntity certInfoEntity) {
        try {
            File file=new File(url+"/certInfo.txt");
            IOUtils.write(certInfoEntity.toString(),new FileOutputStream(file), Charset.defaultCharset());
        } catch (IOException e) {
            throw new PayErrorException(new KftPayError("uploadError","文件创建错误:"+e.getMessage()));
        }
    }
}
